package org.yang.common.core.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.yang.common.i18n.enums.RI18nEnum;
import org.yang.common.i18n.result.ResultI18n;

import java.io.Serializable;

/**
 * 通用请求返回
 *
 * @author: Islands
 * @create: 2024-03-29 17:27
 */
@Data
@NoArgsConstructor
@JsonPropertyOrder(value = {"flag", "code","message","data"})
public class R<T> extends ResultI18n implements Serializable {


    /**
     * 请求是否成功
     */
    private Boolean flag;
    /**
     * 状态码
     * @mock 2000
     * @apiNote  20* <!--成功-->;40* <!--失败-->;50* <!--错误-->
     */
    private int code;
    /**
     * 返回数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;


    public R(Boolean flag, int code, String defaultMsg) {
        super(defaultMsg);
        this.flag = flag;
        this.code = code;
    }
    public R(Boolean flag, RI18nEnum ri18nEnum) {
        super(ri18nEnum);
        this.flag = flag;
        this.code = ri18nEnum.getCode();
    }
    public R(Boolean flag,RI18nEnum ri18nEnum, T data) {
        super(ri18nEnum);
        this.flag = flag;
        this.code = ri18nEnum.getCode();
        this.data = data;
    }
    public R(RI18nEnum nEnum, String... local) {
        super(nEnum, local);
        this.flag = false;
        this.code = nEnum.getCode();
    }


    //TODO 200*
    public static <T> R<T> success() {
        return new R<T>(true,RI18nEnum.SUCCESS,null);
    }
    public static <T> R<T> success(RI18nEnum nEnum) {
        return new R<T>(true,nEnum,null);
    }
    public static <T> R<T> success(T data) {
        return new R<T>(true,RI18nEnum.SUCCESS,data);
    }
    public static <T> R<T> success(RI18nEnum nEnum,T data) {
        return new R<T>(true,nEnum,data);
    }
    //TODO 400*
    public static <T> R<T> fail() {
        return new R<T>(false, RI18nEnum.FAIL);
    }
    public static <T> R<T> fail(String defaultMsg) {
        return new R<T>(false,RI18nEnum.FAIL.getCode(), defaultMsg);
    }
    public static <T> R<T> fail(RI18nEnum nEnum) {
        return new R<T>(false, nEnum);
    }
    public static <T> R<T> fail(RI18nEnum nEnum,String... local) {
        return new R<T>(nEnum,local);
    }
    //TODO 50**
    public static <T> R<T> error() {
        return new R<T>(false,RI18nEnum.ERROR);
    }
    public static <T> R<T> error(String defaultMsg) {
        return new R<T>(false,RI18nEnum.ERROR.getCode(), defaultMsg);
    }
    public static <T> R<T> error(RI18nEnum nEnum) {
        return new R<T>(false,nEnum);
    }

}
